//
//  YZArchieveVC.m
//  VET
//
//  Created by 刘文靖 on 2018/7/15.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZArchieveVC.h"
#import "YZOptionBtn.h"
#import "YZArchieveOptionCell.h"
#import "YZArchieveOptionVC.h"
#import "YZPigModel.h"
#import "YZSwineryModel.h"
#import "YZWorkNoteModel.h"
@interface YZArchieveVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<YZArchiveModel *> *pigArr;
@property (nonatomic, strong) NSMutableArray <YZArchiveModel *>*swineryArr;
@property (nonatomic, strong) NSMutableArray *piggeryArr;
@property (nonatomic, strong) UIView *selectedView;
@property (nonatomic, strong) YZOptionBtn *selecteBtn;
@property (nonatomic, assign) BOOL allSelected;
@property (nonatomic, strong) NSMutableArray *selectedPigs;
@property (nonatomic, strong) NSMutableArray *selectedSwineries;
@property (nonatomic, strong) NSMutableArray <NSMutableArray *>*dataSource;
@end

@implementation YZArchieveVC
- (instancetype)init
{
    self = [super init];
    if (self) {
        _tableView = [UITableView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationBar];
    [self setupMainView];
}

- (void)setupNavigationBar{
    self.title = @"选择归档对象";
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 36, 25)];
    [rightButton setTitle:@"确定" forState:UIControlStateNormal];
    [rightButton setTitleColor:kColorWithHex(0x32BE60) forState:UIControlStateNormal];
    [rightButton setTitleColor:kColorWithHex(0xcecece) forState:UIControlStateDisabled];
    rightButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [rightButton addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *righBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = righBarButtonItem;
}

- (void)setupMainView{
    self.view.backgroundColor = kColorWithHex(0xE9EAEB);
    UIButton *piggeryBtn = [self createButtonWithTitle:@"归属到猪场"];
    [piggeryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(12);
        make.height.mas_equalTo(54);
    }];
    
    UIButton *pigBtn = [self createButtonWithTitle:@"归属到猪只"];
    [pigBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.mas_equalTo(piggeryBtn.mas_bottom).offset(12);
        make.height.mas_equalTo(54);
    }];
    
    UIButton *swineryBtn = [self createButtonWithTitle:@"归属到猪群"];
    [swineryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.mas_equalTo(pigBtn.mas_bottom).offset(12);
        make.height.mas_equalTo(54);
    }];
    
    self.selectedView = [UIView new];
    self.selectedView.backgroundColor = [UIColor clearColor];
    self.selectedView.hidden = YES;
    [self.view addSubview:self.selectedView];
    [self.selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(swineryBtn.mas_bottom);
        make.left.right.offset(0);
        make.height.mas_equalTo(106);
    }];
    UILabel *selectedLabel = [[UILabel alloc] init];
    selectedLabel.text = @"已选择";
    selectedLabel.textColor = kColorWithHex(0x9B9B9B);
    selectedLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [self.selectedView addSubview:selectedLabel];
    [selectedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(25);
        make.top.offset(24);
    }];
    
    YZOptionBtn *selectBtn = [[YZOptionBtn alloc] init];
    selectBtn.descStr = @"全选";
    selectBtn.selected = YES;
    self.allSelected = YES;
    [self.selectedView addSubview:selectBtn];
    [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.mas_equalTo(selectedLabel.mas_bottom).offset(8);
        make.height.mas_equalTo(54);
    }];
    self.selecteBtn = selectBtn;
    //猪场 猪只 猪群
    _dataSource = [NSMutableArray arrayWithCapacity:3];
    [self convertDataSource];
    _tableView.frame = CGRectMake(0, 304, kScreenWidth, kScreenHeight - 304);
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 67, 0);
    _tableView.scrollIndicatorInsets = _tableView.contentInset;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView.backgroundColor = [UIColor clearColor];
    //_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.rowHeight = 54;
    _tableView.sectionHeaderHeight = 12;
    _tableView.tableFooterView = [UIView new];
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView registerClass:[YZArchieveOptionCell class] forCellReuseIdentifier:@"YZArchieveOptionCell"];
    [self.view addSubview:_tableView];
    
    @weakify(self)
    [[self.selecteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        self.selecteBtn.selected = !self.selecteBtn.selected;
        self.allSelected = self.selecteBtn.selected;
        if (self.allSelected) {
            self.selectedSwineries = [NSMutableArray arrayWithArray:self.swineryArr];
            self.selectedPigs = [NSMutableArray arrayWithArray:self.pigArr];
        }else{
            [self.selectedSwineries removeAllObjects];
            [self.selectedPigs removeAllObjects];
        }
        [self.tableView reloadData];
        
    }];
    [[piggeryBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self pushToOptionVC:YZArchieveOptionPiggery];
    }];
    [[pigBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self pushToOptionVC:YZArchieveOptionPig];
    }];
    [[swineryBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self pushToOptionVC:YZArchieveOptionSwinery];
    }];
}

- (UIButton *)createButtonWithTitle:(NSString *)title{
    UIButton *btn= [[UIButton alloc] init];
    btn.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:btn];
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = kColorWithHex(0x393939);;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];;
    label.text = title;
    [btn addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(17);
    }];
    UIImageView *arrowIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ico_right"]];
    [btn addSubview:arrowIV];
    [arrowIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(-18);
    }];
    return btn;
}

- (void)pushToOptionVC:(YZArchieveOptionType)optionType{
    YZArchieveOptionVC *optionVC = [[YZArchieveOptionVC alloc] initWithOptionType:optionType];
    optionVC.selectedModels = self.archiveList;
    @weakify(self)
    optionVC.archieveSuccessBlock = ^(NSArray *selectedArr) {
        @strongify(self)
        YZArchiveModel *archieve = selectedArr.firstObject;
        if ([archieve.archiveType isEqualToString:@"herd"]) {//猪群
            self.swineryArr = [NSMutableArray arrayWithArray:selectedArr];
        }else{//猪只
            self.pigArr = [NSMutableArray arrayWithArray:selectedArr];
        }
        [self convertDataSource];
        
    };
    [self.navigationController pushViewController:optionVC animated:YES];
}

- (void)convertDataSource{
    [_dataSource removeAllObjects];
    if (self.piggeryArr.count){
        [_dataSource addObject:self.piggeryArr];
    }
    if (self.pigArr.count) {
        self.selectedPigs = [NSMutableArray arrayWithArray:self.pigArr];
        [_dataSource addObject:self.pigArr];
    }
    if (self.swineryArr.count){
        self.selectedSwineries = [NSMutableArray arrayWithArray:self.swineryArr];
        [_dataSource addObject:self.swineryArr];
    }
    self.selectedView.hidden = !self.dataSource.count;
    [self.tableView reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZArchieveOptionCell *cell= [tableView dequeueReusableCellWithIdentifier:@"YZArchieveOptionCell"];
    cell.archieveModel = self.dataSource[indexPath.section][indexPath.row];
    cell.optionBtn.selected = self.allSelected;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YZArchieveOptionCell *cell = (YZArchieveOptionCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.optionBtn.selected = !cell.optionBtn.selected;
    [self updateSelectedModel:cell.archieveModel];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)confirmBtnClick{
    if (self.archieveSuccessBlock) {
        NSMutableArray *array = [NSMutableArray array];
        [array addObjectsFromArray:self.selectedSwineries];
        [array addObjectsFromArray:self.selectedPigs];
        self.archieveSuccessBlock(array);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)updateSelectedModel:(YZArchiveModel *)archieveModel{
    if ([archieveModel.archiveType isEqualToString:@"swine"]) {
        for (YZArchiveModel *archive in self.selectedPigs) {
            if (archive.archiveId == archieveModel.archiveId) {
                [self.selectedPigs removeObject:archieveModel];
                return;
            }
        }
        [self.selectedPigs addObject:archieveModel];
    }else{
        for (YZArchiveModel *archive in self.selectedSwineries) {
            if (archive.archiveId == archieveModel.archiveId) {
                [self.selectedSwineries removeObject:archieveModel];
                return;
            }
        }
        [self.selectedSwineries addObject:archieveModel];
    }
}

- (void)setArchiveList:(NSMutableArray *)archiveList{
    _archiveList = archiveList;
    _pigArr = [NSMutableArray array];
    _swineryArr = [NSMutableArray array];
    for (YZArchiveModel *archieve in archiveList) {
        if ( [archieve.archiveType isEqualToString:@"swine"]) {//猪只
            [self.pigArr addObject:archieve];
        }else{//猪群
            [self.swineryArr addObject:archieve];
        }
    }
}

@end
